﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;

namespace EnterpriseServices.Host
{
    partial class WinSvc : ServiceBase
    {

        List<ServiceHost> _hosts = new List<ServiceHost>();

        public WinSvc()
        {
            InitializeComponent();
            ServiceHost _host = null;
            try
            {
                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceRemoting));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceBasic));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceJSON));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceREST));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceRemoting));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceBasic));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceJSON));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceREST));
                _hosts.Add(_host);
            }
            catch (Exception ex)
            {
                eventLog.WriteEntry(String.Format("Failed to start WCF service:\n\n{0}\n\n{1}", ex.Message, ex.StackTrace),
                    EventLogEntryType.Error);
                throw ex;
            }
        }

        protected override void OnStart(string[] args)
        {
            eventLog.WriteEntry("Try start the service...");

            _hosts.ForEach(host => {
                try
                {
                    if (host.State != CommunicationState.Opened)
                        host.Open();

                    StringBuilder _stringBuilder = new StringBuilder();
                    host.Description.Endpoints.ToList().ForEach(endpoint =>
                        _stringBuilder.Append(endpoint.Contract.Name).Append(": ").Append(endpoint.ListenUri.ToString()).Append("\n"));

                    eventLog.WriteEntry(String.Format("WCF host started successfully:\n{0}", _stringBuilder.ToString()));
                }
                catch (Exception ex)
                {
                    eventLog.WriteEntry(String.Format("Failed to start WCF service:\n\n{0}\n\n{1}", ex.Message, ex.StackTrace),
                        EventLogEntryType.Error);
                }
            });
        }

        protected override void OnStop()
        {
            _hosts.ForEach(host => {
                try
                {
                    if (host.State != CommunicationState.Closed)
                        host.Close();
                    eventLog.WriteEntry(String.Format("WCF host stopped successfully:\n\n{0}", host.Description));
                }
                catch (Exception ex)
                {
                    host.Abort();
                    eventLog.WriteEntry(String.Format("Failed to stop WCF service.\nThe WCF service was aborted.:\n\n{0}\n\n{1}\n\n{2}", host.Description, ex.Message, ex.StackTrace),
                        EventLogEntryType.Error);
                }
            });
        }
    }
}
